package com.bt7274.service.impl;

import com.bt7274.mapper.ForumMapper;
import com.bt7274.pojo.Forum;
import com.bt7274.service.ForumService;
import com.bt7274.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ForumServiceImpl implements ForumService {

    @Autowired
    private ForumMapper forumMapper;

    @Override
    public void createForum(Forum forum) {
        forum.setId(UUID.randomUUID().toString().replace("-", ""));
        forum.setCreatedBy(SessionUtils.getCurrentUserId());  // 当前登录用户
        forumMapper.insertForum(forum);
    }

    @Override
    public void deleteForum(String id) {
        forumMapper.deleteForumById(id);
    }

    @Override
    public void renameForum(String id, String newName) {
        forumMapper.updateForumName(id, newName);
    }

    @Override
    public void moveForum(String id, String newParentId) {
        forumMapper.updateParentId(id, newParentId);
    }

    @Override
    public List<Forum> getAllForums() {
        return forumMapper.selectAllForums();
    }

    @Override
    public Forum getForumById(String id) {
        return forumMapper.selectForumById(id);
    }

    public void reDescription(String id, String description){
        Forum forum = forumMapper.selectForumById(id);
        if (forum != null) {
            forum.setDescription(description);
            forumMapper.updateForumDescription(id, description);
        } else {
            throw new RuntimeException("Forum not found with id: " + id);
        }
    }
}
